/** Decolorizer
* Removes the color component of an image. 
* The saturation can be mixed from a fixed value (saturation) with the
* saturation in the image (mixing factor: satPercent). 
* Usage: infile:String colorangle:Float saturation:Float satPercent:Float [outfile:String]
* Example: C:\Data\Pictures\bubble_bobble_06.png 0.1 1.0 0.5
*/

// import common api
import org.pi.common.Api._

// check parameters of script
if (args.length!=5 && args.length!=4) throw new Error("usage: infile, colorangle, saturation, satPercent, [outfile]")

// load input image
val a = imread(args(0))

// start: measure time
tic

// convert to hsv color space
val b = rgb2hsv(a)

b(~~,~~,0) = args(1).toFloat
b(~~,~~,1) = (b(~~,~~,1) * args(3).toFloat) + (args(2).toFloat * (1.0f-args(3).toFloat))
	
// convert back to rgb colorspace
val c = hsv2rgb(b)

// end: measure time
toc("decolorize") 

// save output image
if (args.length==5) {
	imwrite(args(4),c)
}
else {
	subplot(2,1,0)
	imshow(a)
	subplot(2,1,1)
	imshow(c)
}

